﻿/*************************** Copyright *****************************\
 * Copyright 2011 Sendine.com - http://www.sendine.com/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
\*******************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
namespace Sendine.Web.Route.Cfg
{
    /// <summary>
    /// 连接路由规则
    /// </summary>
    public class RouterRule
    {
        public const string cRouterRule = "rule";
        public const string cRuleRegex = "regex";
        public const string cRuleTarget = "target";
        public const string cRuleValidator = "validator";
        public const string cAssembly = "assembly";
        public const string cReplace = "replacement";
        public const string cTrim = "trim";

        protected string currentTarget;
        protected string subPath;
        protected string hostName;
        protected string handlerName;
        protected string assemblyName;
        protected string validatorName;
        protected string trim;
        protected Dictionary<string, string> replacement;
        protected string replacementString;

        private string regex;
        private string target;

        /// <summary>
        /// 正则表达式
        /// </summary>
        public string Regex { get { return regex; } set { regex = value; } }

        /// <summary>
        /// 获取目标Url
        /// </summary>
        public string Target { get { return target; } set { target = value; } }

        /// <summary>
        /// 主机指向的目录
        /// </summary>
        public string Host { get { return hostName; } set { hostName = value; } }

        /// <summary>
        /// 
        /// </summary>
        public string Trim { get { return trim; } set { trim = value; } }

        /// <summary>
        /// 主机指向的目录
        /// </summary>
        public string AssemblyName { get { return assemblyName; } set { assemblyName = value; } }

        /// <summary>
        /// 
        /// </summary>
        public bool IsRegexValidator { get { return string.IsNullOrEmpty(validatorName); } }

        /// <summary>
        /// 
        /// </summary>
        public Dictionary<string, string> Replacement { get { return replacement; } }

        /// <summary>
        /// 
        /// </summary>
        public string ReplacementString { get { return replacementString; } }

        /// <summary>
        /// 处理程序全称
        /// </summary>
        public string HandlerName
        {
            get { return handlerName; }
            set { handlerName = value; }
        }


        /// <summary>
        /// <see cref="RuleValidator"/>对象
        /// </summary>
        public string ValidatorName { get { return validatorName; } }

        public RouterRule()
        {
            replacement = new Dictionary<string, string>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="regex"></param>
        /// <param name="target"></param>
        public RouterRule(string regex
                        , string target
                        , string validatorName
                        , string assemblyName
                        , string hostname
                        , string replace
                        , string trim)
        {
            this.regex = regex;
            this.target = target;
            this.hostName = hostname;
            this.assemblyName = assemblyName;
            this.validatorName = validatorName;
            this.trim = trim;
            this.SetReplacement(replace);
        }

        public void SetReplacement(string replace)
        {
            replacementString = replace;
            replacement = new Dictionary<string, string>();

            if (!string.IsNullOrEmpty(replace))
            {
                string[] kvpair = replace.Split(',');
                foreach (string kv in kvpair)
                {
                    string[] args = kv.Split(' ');
                    replacement.Add(args[0], args[1]);
                }
            }
        }
    }
}
